package com.wwtech.epp.ui.activity.friend;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.socks.library.KLog;
import com.wwtech.epp.R;
import com.wwtech.epp.application.AppConfig;
import com.wwtech.epp.base.BaseActivity;
import com.wwtech.epp.contain.ContainValue;
import com.wwtech.epp.db.Friend;
import com.wwtech.epp.ui.activity.friend.component.DaggerFriendListComponent;
import com.wwtech.epp.ui.activity.friend.contract.FriendListContract;
import com.wwtech.epp.ui.activity.friend.module.FriendListModule;
import com.wwtech.epp.ui.activity.friend.presenter.FriendListPresenter;
import com.wwtech.epp.ui.activity.mine.MyInfoActivity;
import com.wwtech.epp.ui.adapter.main.GroupMemberCheckMangerAdapter;
import com.wwtech.epp.utils.ToastUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author hzp
 * @Package com.wwtech.epp.ui.activity.friend
 * @Description: $description
 * @date 2017/09/25 15:56:45
 */

public class FriendListActivity extends BaseActivity implements FriendListContract.View, BaseQuickAdapter.RequestLoadMoreListener, GroupMemberCheckMangerAdapter.OnCheckChange {

    @Inject
    FriendListPresenter mPresenter;
    @Inject
    GroupMemberCheckMangerAdapter groupMemberCheckMangerAdapter;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    private int mNextPage = 1;
    private int onePageSize = 10;
    private int total;
    private String nodeId = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initView() {
        setContentView(R.layout.activity_friend_list);
        ButterKnife.bind(this);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(groupMemberCheckMangerAdapter);
        groupMemberCheckMangerAdapter.setOnLoadMoreListener(this, recyclerView);
        groupMemberCheckMangerAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                if (("" + ContainValue.userLogin.getData().getUserId()).equals(groupMemberCheckMangerAdapter.getData().get(i).getUserId())) {
                    Intent intent = new Intent(FriendListActivity.this, MyInfoActivity.class);
                    startActivity(intent);
                } else {
                    Intent intent = new Intent(FriendListActivity.this, UserDetailActivity.class);
                    intent.putExtra("friend", groupMemberCheckMangerAdapter.getData().get(i));
                    startActivity(intent);
                }
//                Intent intent = new Intent(FriendListActivity.this, UserDetailActivity.class);
//                intent.putExtra("friend", groupMemberCheckMangerAdapter.getData().get(i));
//                startActivity(intent);
            }
        });
    }

    @Override
    protected void initData() {
        if (getIntent().getStringExtra("name").equals("删除群成员")) {
            groupMemberCheckMangerAdapter.setCheckMode(true);
            groupMemberCheckMangerAdapter.setOncheckChange(this);
        } else {
        }
        nodeId = getIntent().getStringExtra("id");
        setTitle(getIntent().getStringExtra("name"));
        mNextPage = 1;
        if (getIntent().getStringExtra("flag").equals("zuzhi")) {
            total = getIntent().getIntExtra("total", 0);
            KLog.i(total);
            Map<String, String> map = new HashMap<>();
            map.put("pageSize", onePageSize + "");
            map.put("pageNum", mNextPage + "");
            mPresenter.getNodeMember(nodeId, map);
        } else {
            Map<String, String> map = new HashMap<>();
            map.put("pageSize", "1000");
            map.put("pageNum", mNextPage + "");
            mPresenter.getGroupMember(getIntent().getStringExtra("id") + "", map);
        }

    }

    /**
     * 从网络拉取的联系人的集合处理完的了的数据的回调，会把这些数据直接显示在recycleview上面
     *
     * @param list
     */
    @Override
    public void groupMemberBack(ArrayList<Friend> list) {
        if (getIntent().getStringExtra("name").equals("删除群成员")) {
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getUserId().equals(ContainValue.userLogin.getData().getUserId() + "")) {
                    list.remove(i);
                    break;
                }
            }
        }
        if (mNextPage == 1) {
            groupMemberCheckMangerAdapter.setNewData(list);
        } else {
            groupMemberCheckMangerAdapter.loadMoreComplete();
            groupMemberCheckMangerAdapter.addData(list);
        }
    }

    @Override
    public void groupMemberBack1(ArrayList<Friend> list) {
        ArrayList<Friend> friendArrayList = new ArrayList<>();
//        for (int i = 0; i < list.size(); i++) {
//            //String userId, String name, Uri portraitUri, String phoneNumber, String displayName
//            Friend friend = new Friend(list.get(i).getUserId() + "", list.get(i).getNickName(), Uri.parse(list.get(i).getAvatar()), list.get(i).getPhone(), list.get(i).getNickName());
//            friend.setAvatar(list.get(i).getAvatar());
//            friend.setNickname(list.get(i).getNickName());
//            friendArrayList.add(friend);
//            groupMemberCheckMangerAdapter.setNewData(friendArrayList);
//        }
    }

    @Override
    public void setTotal(int total) {
        this.total = total;
    }

    @Override
    protected void setupActivityComponent() {
        DaggerFriendListComponent
                .builder()
                .appComponent(((AppConfig) getApplication()).getApplicationComponent())
                .friendListModule(new FriendListModule(this))
                .build()
                .inject(this);
    }

    @Override
    public void setPresenter(FriendListContract.FriendListContractPresenter presenter) {
        mPresenter = (FriendListPresenter) presenter;
    }

    @Override
    public void showProgressDialog() {
        progressDialog.show();
    }

    @Override
    public void closeProgressDialog() {
        progressDialog.hide();
    }

    @Override
    public void onLoadMoreRequested() {
        KLog.i("请求加载更多。。。");
        if (groupMemberCheckMangerAdapter.getData().size() < onePageSize || groupMemberCheckMangerAdapter.getData().size() >= total) {
            groupMemberCheckMangerAdapter.loadMoreEnd(true);
            return;
        }
        mNextPage ++;
        KLog.i("开始请求加载更多。。。");
        Map<String, String> map = new HashMap<>();
        map.put("pageSize", onePageSize + "");
        map.put("pageNum", mNextPage + "");
        mPresenter.getNodeMember(nodeId, map);
    }

    @Override
    public void onCheckChange(Friend friend, boolean isChecked) {
        if (ContainValue.partPerson == null) {
            ContainValue.partPerson = new ArrayList<>();
        }
        for (int i = 0; i < ContainValue.partPerson.size(); i++) {
            if (friend.getUserId().equals(ContainValue.partPerson.get(i).getUserId())) {
                if (isChecked) {
                    return;
                } else {
                    ContainValue.partPerson.remove(i);
                    return;
                }
            }
        }
        ContainValue.partPerson.add(friend);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if (getIntent().getStringExtra("name").equals("删除群成员")) {
            getMenuInflater().inflate(R.menu.over_menu, menu);
        }
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.over:
                if (ContainValue.partPerson == null || ContainValue.partPerson.size() == 0) {
                    ToastUtil.displayShortToast("没有选择群成员");
                    return true;
                } else {
                    setResult(20);
                    finish();
                    return true;
                }
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}